<template>
  <el-dialog
    v-model="props.btgDialog"
    title="不通过"
    width="25%"
    :show-close="false"
  >
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="不通过原因" prop="reson">
        <el-input
          v-model="ruleForm.reson"
          placeholder="请输入原因"
          type="textarea"
          show-word-limit
          maxlength="64"
          row="4"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="quxiao(ruleFormRef)"> 取消 </el-button>
        <el-button
          type="primary"
          @click="queren(ruleFormRef)"
          :loading="loading"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, ElTree, FormInstance, FormRules } from 'element-plus'
import { ref, reactive, watch } from 'vue'
import { auditPartApplyApi, auditPartApplyDetailApi } from '/@/api/spare'
const props = defineProps({
  btgDialog: Boolean,
  btgForm: Object
})
const emit = defineEmits(['quxiaoBTG', 'submitBTG'])
const formSize = ref('default')
const loading = ref(false)
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref({
  reson: ''
})
const rules = reactive<FormRules>({
  reson: [{ required: true, message: '请输入不通过原因', trigger: 'blur' }]
})

const quxiao = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('quxiaoBTG')
}
const queren = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      if (props.btgForm?.type == '1') {
        let params = {
          applyId: props.btgForm?.id,
          auditResult: 2,
          reason: ruleForm.value.reson
        }
        auditPartApplyApi(params).then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '已拒绝申请'
            })
            emit('submitBTG')
          }
        })
      } else {
        let params = {
          applyDetailId: props.btgForm?.id,
          auditResult: 2,
          reason: ruleForm.value.reson
        }
        auditPartApplyDetailApi(params).then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '已拒绝申请'
            })
            emit('submitBTG')
          }
        })
      }
      loading.value = false
    } else {
      loading.value = false
    }
  })
}

watch(
  () => props.btgDialog,
  () => {
    console.log(props.btgForm)
  }
)
</script>
<style scoped lang="scss"></style>
